#ifndef RISCV_H__
#define RISCV_H__

#include <stdint.h>

static inline uint8_t  inb(uintptr_t addr) { return *(volatile uint8_t  *)addr; }
static inline uint16_t inw(uintptr_t addr) { return *(volatile uint16_t *)addr; }
static inline uint32_t inl(uintptr_t addr) { return *(volatile uint32_t *)addr; }
#if defined(__ISA_RISCV64__)
static inline uint64_t ind(uintptr_t addr) { return *(volatile uint64_t *)addr; }
static inline void outd(uintptr_t addr, uint64_t data) { *(volatile uint64_t *)addr = data; }
#endif

static inline void outb(uintptr_t addr, uint8_t  data) { *(volatile uint8_t  *)addr = data; }
static inline void outw(uintptr_t addr, uint16_t data) { *(volatile uint16_t *)addr = data; }
static inline void outl(uintptr_t addr, uint32_t data) { *(volatile uint32_t *)addr = data; }

#define PTE_V 0x01
#define PTE_R 0x02
#define PTE_W 0x04
#define PTE_X 0x08
#define PTE_U 0x10
#define PTE_A 0x40
#define PTE_D 0x80
enum {
  MCAUSE_INTR = 1ull << 63,
  MCAUSE_ECALL_M = 11|MCAUSE_INTR,
  MCAUSE_TIM_INT_M = 7|MCAUSE_INTR,
};
enum {
  MSTATUS_MIE_MASK = 1 << 3,
  MSTATUS_MPIE_MASK = 1 << 7,
  MSTATUS_MPP_MASK = 3 << 11,
  MIE_MTIE_MASK = 1 << 7,
  MIP_MTIP_MASK = 1 << 7,
  MIP_MSIP_MASK = 1 << 3,
};

enum { MODE_U, MODE_S, MODE_M = 3 };
#define MSTATUS_MXR  (1 << 19)
#define MSTATUS_SUM  (1 << 18)

#if __riscv_xlen == 64
#define MSTATUS_SXL  (2ull << 34)
#define MSTATUS_UXL  (2ull << 32)
#else
#define MSTATUS_SXL  0
#define MSTATUS_UXL  0
#endif

#endif
